﻿using System;
using System.Collections.Generic;

namespace JUtil.CodeGenerator
{
    public partial class ProjectDataGGConfig
    {
        public string NAMESPACE { get; set; }
        public string ConnectionString {get; set;}
        public DbProvider DbProvider {get; set;}
        public DatabaseType DatabaseType {get; set;}
        public List<string> Tables { get; set; }
        public string ExportDirectory{ get; set; }
        public ProjectDataGG.ExportMode ExportMode { get; set; }
        
        public string NAMESPACE_FILENAME
        {
            get
            {
                return NAMESPACE.Replace(".", "_");
            }
        }
    }

    #region ProjectDataGG
    public class ProjectDataGG
    {
        [Flags]
        public enum ExportMode
        {
            None = 0,
            Sql = 1,
            ProjectBase = 2,
            GenDataFiles = 4,
            DalFiles = 8,
            Main = 16,
            All = Sql | ProjectBase | GenDataFiles | DalFiles | Main
        }

        public ProjectDataGG(ProjectDataGGConfig config)
        {
            this.config = config;
        }

        private readonly ProjectDataGGConfig config;

        private void ExpSql()
        {
            string filename = string.Format("{0}.sql", config.NAMESPACE_FILENAME + "_DataGenerator");
            string directory = JUtil.Path.File.GetAbsolutePath(config.ExportDirectory, "Sql");
            JUtil.Path.Directory.MakeSureExists(directory);
            string filepath = JUtil.Path.File.GetAbsolutePath(directory, filename);

            GenTableDataGenericGeneratorSql generator = new GenTableDataGenericGeneratorSql(config);
            generator.Export(filepath);
        }

        private void ExpProjectBase()
        {
            string filename = string.Format("{0}.cs", config.NAMESPACE_FILENAME + "_DataGenerator");
            string directory = JUtil.Path.File.GetAbsolutePath(config.ExportDirectory, "Core");
            JUtil.Path.Directory.MakeSureExists(directory);
            string filepath = JUtil.Path.File.GetAbsolutePath(directory, filename);

            GenTableDataGenericGenerator generator = new GenTableDataGenericGenerator(config);
            generator.Export(filepath);
        }

        private void ExpGenDataFiles()
        {
            foreach (string table in config.Tables)
            {
                string filename = string.Format("Gen{0}.cs", table);
                string directory = JUtil.Path.File.GetAbsolutePath(config.ExportDirectory, "Gen");
                JUtil.Path.Directory.MakeSureExists(directory);
                string filepath = JUtil.Path.File.GetAbsolutePath(directory, filename);

                GenTableDataGenerator generator = new GenTableDataGenerator(config.NAMESPACE, table);
                generator.Export(filepath);
            }
        }

        private void ExpDalFiles()
        {
            IDbExtraOp database = new XDatabase(config.DbProvider, config.ConnectionString);

            foreach (string table in config.Tables)
            {
                string filename = string.Format("{0}.cs", table);
                string directory = JUtil.Path.File.GetAbsolutePath(config.ExportDirectory, "Dal");
                JUtil.Path.Directory.MakeSureExists(directory);
                string filepath = JUtil.Path.File.GetAbsolutePath(directory, filename);

                string sql = string.Format("select * from {0}", table);

                DalGenerator generator = new DalGenerator(database, config.NAMESPACE, table, sql);
                generator.Export(filepath);
            }
        }

        private void ExpMain()
        {
            string filename = string.Format("Program.cs");
            string filepath = JUtil.Path.File.GetAbsolutePath(config.ExportDirectory, filename);

            GenMainFunction generator = new GenMainFunction(config);
            generator.Export(filepath);
        }

        public void Export()
        {
            ExportMode ExportFiles = config.ExportMode;

            if((ExportFiles & ExportMode.Sql) == ExportMode.Sql)
                ExpSql();

            if ((ExportFiles & ExportMode.ProjectBase) == ExportMode.ProjectBase)
                ExpProjectBase();

            if ((ExportFiles & ExportMode.GenDataFiles) == ExportMode.GenDataFiles)
                ExpGenDataFiles();

            if ((ExportFiles & ExportMode.DalFiles) == ExportMode.DalFiles)
                ExpDalFiles();

            if ((ExportFiles & ExportMode.Main) == ExportMode.Main)
                ExpMain();
        }

        public static string Instruction()
        {
            string strInstruction = @"
        public static void Instruction()
        {
            new ProjectDataGG(new ProjectDataGGConfig
            {
                NAMESPACE = ""A.B.C"",
                ConnectionString = ""Data Source=tppddb;User ID=tpdbuser;Password=tpdbuser;"",
                DbProvider = DbProvider.Oracle,
                DatabaseType = DatabaseType.Oracle,
                Tables = new List<string>
                {
                    ""U_BBS_PARENT"",
                    ""U_BBS_SUBJECT"",
                    ""U_BBS_DETAIL"",
                    ""U_QUESTIONNAIRE_MAIN"",
                    ""U_QUESTIONNAIRE_PARENT"",
                    ""U_QUESTIONNAIRE_OPTION"",
                    ""U_QUESTIONNAIRE_QUEUE""
                },
                ExportDirectory = @""R:\"",
            }).Export();
        }
            ";

            string strInstructionForLog = strInstruction.Replace("{", "{{").Replace("}", "}}");

            return strInstructionForLog;
        }

    }
    #endregion


}
